package com.imonsoft.sqm.dao.business.supplier_quality;

import com.imonsoft.common.constant.ConstantDict;
import com.imonsoft.common.constant.ConstantSqm;
import com.imonsoft.common.httpModel.DataGrid;
import com.imonsoft.common.httpModel.Pager;
import com.imonsoft.common.util.DateUtil;
import com.imonsoft.common.util.StringUtil;
import com.imonsoft.sqm.pojo.basedata.Dictionary;
import com.imonsoft.sqm.pojo.business.supplier_quality.QualitySystem;
import com.imonsoft.sqm.pojo.business.supplier_quality.QualitySystemExample;
import com.imonsoft.sqm.service.basedata.DictionaryService;
import com.imonsoft.sqm.vo.business.supplier_quality.QualitySystemVo;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import org.springframework.stereotype.Repository;
@SuppressWarnings("deprecation")
@Repository("qualitySystemDAO")
public class QualitySystemDAOImpl extends SqlMapClientDaoSupport implements QualitySystemDAO {

	
	@Autowired
	private DictionaryService dictionaryService;
    /**
     * This method was generated by Apache iBATIS ibator.
     * This method corresponds to the database table tt_quality_system
     *
     * @ibatorgenerated Tue Jun 04 21:35:12 CST 2013
     */
    public QualitySystemDAOImpl() {
        super();
    }

    /**
     * This method was generated by Apache iBATIS ibator.
     * This method corresponds to the database table tt_quality_system
     *
     * @ibatorgenerated Tue Jun 04 21:35:12 CST 2013
     */
    public int countQualitySystemByExample(QualitySystemExample example) {
        Integer count = (Integer)  getSqlMapClientTemplate().queryForObject("tt_quality_system.ibatorgenerated_countByExample", example);
        return count;
    }

    /**
     * This method was generated by Apache iBATIS ibator.
     * This method corresponds to the database table tt_quality_system
     *
     * @ibatorgenerated Tue Jun 04 21:35:12 CST 2013
     */
    public int deleteQualitySystemByExample(QualitySystemExample example) {
        int rows = getSqlMapClientTemplate().delete("tt_quality_system.ibatorgenerated_deleteByExample", example);
        return rows;
    }

    /**
     * This method was generated by Apache iBATIS ibator.
     * This method corresponds to the database table tt_quality_system
     *
     * @ibatorgenerated Tue Jun 04 21:35:12 CST 2013
     */
    public int deleteQualitySystemByPrimaryKey(Long id) {
        QualitySystem key = new QualitySystem();
        key.setId(id);
        int rows = getSqlMapClientTemplate().delete("tt_quality_system.ibatorgenerated_deleteByPrimaryKey", key);
        return rows;
    }

    /**
     * This method was generated by Apache iBATIS ibator.
     * This method corresponds to the database table tt_quality_system
     *
     * @ibatorgenerated Tue Jun 04 21:35:12 CST 2013
     */
    public Long insertQualitySystem(QualitySystem record) {
        Object newKey = getSqlMapClientTemplate().insert("tt_quality_system.ibatorgenerated_insert", record);
        return (Long) newKey;
    }

    /**
     * This method was generated by Apache iBATIS ibator.
     * This method corresponds to the database table tt_quality_system
     *
     * @ibatorgenerated Tue Jun 04 21:35:12 CST 2013
     */
    public Long insertQualitySystemSelective(QualitySystem record) {
        Object newKey = getSqlMapClientTemplate().insert("tt_quality_system.ibatorgenerated_insertSelective", record);
        return (Long) newKey;
    }

    /**
     * This method was generated by Apache iBATIS ibator.
     * This method corresponds to the database table tt_quality_system
     *
     * @ibatorgenerated Tue Jun 04 21:35:12 CST 2013
     */
    @SuppressWarnings("unchecked")
    public List<QualitySystem> selectQualitySystemByExample(QualitySystemExample example) {
        List<QualitySystem> list = getSqlMapClientTemplate().queryForList("tt_quality_system.ibatorgenerated_selectByExample", example);
        return list;
    }

    /**
     * This method was generated by Apache iBATIS ibator.
     * This method corresponds to the database table tt_quality_system
     *
     * @ibatorgenerated Tue Jun 04 21:35:12 CST 2013
     */
    public QualitySystem selectQualitySystemByPrimaryKey(Long id) {
        QualitySystem key = new QualitySystem();
        key.setId(id);
        QualitySystem record = (QualitySystem) getSqlMapClientTemplate().queryForObject("tt_quality_system.ibatorgenerated_selectByPrimaryKey", key);
        return record;
    }

    /**
     * This method was generated by Apache iBATIS ibator.
     * This method corresponds to the database table tt_quality_system
     *
     * @ibatorgenerated Tue Jun 04 21:35:12 CST 2013
     */
    public int updateQualitySystemByExampleSelective(QualitySystem record, QualitySystemExample example) {
        UpdateByExampleParms parms = new UpdateByExampleParms(record, example);
        int rows = getSqlMapClientTemplate().update("tt_quality_system.ibatorgenerated_updateByExampleSelective", parms);
        return rows;
    }

    /**
     * This method was generated by Apache iBATIS ibator.
     * This method corresponds to the database table tt_quality_system
     *
     * @ibatorgenerated Tue Jun 04 21:35:12 CST 2013
     */
    public int updateQualitySystemByExample(QualitySystem record, QualitySystemExample example) {
        UpdateByExampleParms parms = new UpdateByExampleParms(record, example);
        int rows = getSqlMapClientTemplate().update("tt_quality_system.ibatorgenerated_updateByExample", parms);
        return rows;
    }

    /**
     * This method was generated by Apache iBATIS ibator.
     * This method corresponds to the database table tt_quality_system
     *
     * @ibatorgenerated Tue Jun 04 21:35:12 CST 2013
     */
    public int updateQualitySystemByPrimaryKeySelective(QualitySystem record) {
        int rows = getSqlMapClientTemplate().update("tt_quality_system.ibatorgenerated_updateByPrimaryKeySelective", record);
        return rows;
    }

    /**
     * This method was generated by Apache iBATIS ibator.
     * This method corresponds to the database table tt_quality_system
     *
     * @ibatorgenerated Tue Jun 04 21:35:12 CST 2013
     */
    public int updateQualitySystemByPrimaryKey(QualitySystem record) {
        int rows = getSqlMapClientTemplate().update("tt_quality_system.ibatorgenerated_updateByPrimaryKey", record);
        return rows;
    }

    /**
     * This class was generated by Apache iBATIS ibator.
     * This class corresponds to the database table tt_quality_system
     *
     * @ibatorgenerated Tue Jun 04 21:35:12 CST 2013
     */
    private static class UpdateByExampleParms extends QualitySystemExample {
        private Object record;

        public UpdateByExampleParms(Object record, QualitySystemExample example) {
            super(example);
            this.record = record;
        }

        public Object getRecord() {
            return record;
        }
    }

	@SuppressWarnings("unchecked")
	@Override
	public DataGrid<QualitySystemVo> getDataGridVoBy(QualitySystem qualitySystem, Pager pager) {
		
		QualitySystemExample example = new QualitySystemExample();
		QualitySystemExample.Criteria criteria = example.createCriteria();
		criteria.andStateNotEqualTo(ConstantSqm.STATUS_DELETE);
		if (!StringUtil.isBlank(StringUtil.fixup(qualitySystem.getSupplierId()))){
			criteria.andSupplierIdEqualTo(qualitySystem.getSupplierId());	
		}
		Integer count=countQualitySystemByExample(example);
		List<QualitySystem> rows =getSqlMapClientTemplate().queryForList("tt_quality_system.ibatorgenerated_selectByExample", example, pager.getSkipResults(), pager.getRows());
		List<QualitySystemVo> list=new ArrayList<QualitySystemVo>();
		
		for (QualitySystem qualitySystem2 : rows) {
			QualitySystemVo vo=new QualitySystemVo();
			BeanUtils.copyProperties(qualitySystem2, vo);
			
			if(qualitySystem2.getCertificationDate()!=null)
			   vo.setCertificationDates(DateUtil.date2string(qualitySystem2.getCertificationDate(), DateUtil.DATE_FORMAT));
			if(qualitySystem2.getExpiredDate()!=null)
			   vo.setExpiredDates(DateUtil.date2string(qualitySystem2.getExpiredDate(), DateUtil.DATE_FORMAT));
			if(qualitySystem2.getSelfAssessmentDate()!=null)
				vo.setSelfAssessmentDates(DateUtil.date2string(qualitySystem2.getSelfAssessmentDate(), DateUtil.DATE_FORMAT));
				
			vo.setMailDates(qualitySystem2.getMailDate());
			
			List<Dictionary> d1=dictionaryService.findDictionaryByType(ConstantDict.QuanlitySystem_RZLX);
			for (Dictionary dictionary : d1) {
				if(dictionary.getCode().equals(vo.getCertificationType())){
					vo.setCertificationType(dictionary.getName());
				}
			}
			
			List<Dictionary> d2=dictionaryService.findDictionaryByType(ConstantDict.Quanlity_XD);
			for (Dictionary dictionary : d2) {
				if(dictionary.getCode().equals(vo.getUpdateRequired())){
					vo.setUpdateRequired(dictionary.getName());
				}
			}
			
			List<Dictionary> d3=dictionaryService.findDictionaryByType(ConstantDict.Quanlity_SXD);
			for (Dictionary dictionary : d3) {
				if(dictionary.getCode().equals(vo.getSelfAssessment())){
					vo.setSelfAssessment(dictionary.getName());
				}
			} 
			
			
			list.add(vo);
		}
		
		 DataGrid<QualitySystemVo> dataGrid = new  DataGrid<QualitySystemVo>(count, list);
		return dataGrid;
	}

 
}